<%@page import="model.Target"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Script type="text/javascript">
	window.onload=function(){
	if(document.getElementById("get_name").value==""){
	
	document.getElementById("logout_a").style.display="none";
	}
}

function search(){
	//window.location.href="Logout";
}

</Script>
<!-- *************************js******************************  -->
<input type="hidden" id="get_name" value="${username}"/>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*" %>
<%@ page import="model.Category"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>
<%@ page import="model.Target" %>
<%@ page import="model.Myuser" %>

 
<%
String name=(String)session.getAttribute("username");
String authority = (String) session.getAttribute("authority");

List<Category> cs=(List<Category>)request.getAttribute("cates");
List<Target> default_display=(List<Target>)request.getAttribute("default_display");
List<Target> target_cate=(List<Target>)request.getAttribute("target_cate");
List<Myuser> allusetlist = (List<Myuser>)request.getAttribute("ulist");
int[] userCommentCount = (int[])request.getAttribute("countlist");

String cnum = (String)request.getAttribute("commentnum");


if(name==null){
    out.print("Please login first! redirecting to Login...");
    response.setHeader("Refresh","1;URL=login.jsp");
}

else{
%>



<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="main.css">
<title>Home</title>
</head>


<body>

<center>
<h1>Weekend Planer -- Plan your Weekends.</h1>
<hr>

<table>
<tr style="height:100px;"><td>
<%
try{
if(name!=null&&name!="")
{
out.println("Hi: "+name);
//out.println(cnum);
%>
<br>
<%
out.println("Authority: " + authority);
}
}
catch(Exception e){
	e.printStackTrace();
}
/* request.setAttribute("result_num", result_num);
request.setAttribute("search_result", result);
 */
 String result_num=(String)request.getAttribute("result_num");
 ArrayList<Target> search_result=(ArrayList<Target>)request.getAttribute("search_result");
 %></td>
<td>
<form action="Search" method="get" >
<input type="text" name="search_box" id="search_box" style="width: 500px;" onkeypress="if(event.keyCode==13) {search_button.click();return false;}" />
<input type="submit" name="search" value="search" id="search_button"  />
</form>
</td>
<td>

<a href="Logout" id="logout_a">Logout</a> <a href="ManageAccount.jsp" id="ManageAcount"> Modify Account</a>
</td></tr>
<tr>
<td style="width:200px;" valign="top">
<table>
<tr><td><h4>Star Users:</h4></td></tr>
<%
try{
	int starUserLimit = 1;
	String unametemp;
	if(allusetlist!=null){
	for(int i=0;i<allusetlist.size();i++){
			
		if(userCommentCount[i]>=starUserLimit){
			unametemp = allusetlist.get(i).getUsername();
			out.print("<tr><td><a>"+unametemp+"<a/></td></tr>");
		}
	} 
	}
 	
}
catch(Exception e){
	e.printStackTrace();
}


%>
<br><br>

<tr><td><h4>Categories:</h4>

<%
try{
	
 	if(cs!=null){
 		if(cs.size()==0){
 			out.print("<tr><td>No result found.</td></tr>");	
 		}
 		else{
		for(Category c:cs){
			String url=c.getCategoryid()+"";;
			out.print("<tr><td><a href=\"Home?categ="+url+"\">"+c.getCategoryname()+"<a/></td></tr>");
		}
 		}
	} 
 	
}
catch(Exception e){
	e.printStackTrace();
}

%>

</td></tr>




</table>
</td>

<td style="width:600px;" valign="top">
<table style="width:600px;">
<tr><td>main display area:</td></tr>
	<%
	
	List<Target> show=null;
	try{
	if(search_result!=null){
		show=search_result;
	}else if(target_cate!=null){
		show=target_cate;
	}else{
		show=default_display;
	}
	if(show==null){
		response.sendRedirect("Home");
	}
	else if(show.size()==0){
		out.print("<tr><td><h4>Not Found!</h4></td></tr>");
	}
	else{
	for(Target t:show){
		String t_name = t.getTargetname();
		String targetID = Integer.toString(t.getTargetid());
	%>	<tr><td><a href="TargetServlet?targetID=<%=targetID%>"> <% out.print(t.getTargetname() + "<br>"); %> </a> </td></tr>
	
	<%
		}
	}
	}
	catch(Exception e){
		e.printStackTrace();
	}
	%>
	
</table>
</td>


<td style="width:200px;" valign="top">
<table>
<tr><td><h4>Operation:</h4></td></tr>

<% if(authority.equals("admin")){
%>
	<tr><td><a href="AdminServlet">Manage User Accounts</a></td></tr>
<%	}	
%>

<% if(authority.equals("editor") || authority.equals("admin")){
%>
	<tr><td><a href="ReviewApply">Inspect Application</a></td></tr>
<%	}	
%>



<tr><td><a href="MyMerchant">My Merchants</a></td></tr>

<tr><td><a href="LikeList">My Like List</a></td></tr>
<tr><td><a href="Home">Popular</a></td></tr>
</table>
</td></tr>
<tr style="height:150px;"><td></td><td  style="text-align:center;">Powered by GFS Group</td><td></td></tr>
</table>
</center>

</body>
</html>
<%} %>